//
//  EndServiceInsuredVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/5/12.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "EndServiceInsuredVC.h"
#import "IQTextView.h"
#import "RefundInsureVC.h"

@interface EndServiceInsuredVC ()
@property (weak, nonatomic) IBOutlet IQTextView *desTextView;

@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;
@property (weak, nonatomic) IBOutlet UIButton *button4;
@property (weak, nonatomic) IBOutlet UIButton *button5;

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@property (weak, nonatomic) IBOutlet UIImageView *imageView4;
@property (weak, nonatomic) IBOutlet UIImageView *imageView5;
@property(nonatomic,strong) BuyInsuredOrderModel *model;
@property(nonatomic,copy) NSString *reason;
@end

@implementation EndServiceInsuredVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"结束服务";
    self.desTextView.placeholder = @"请描述问题详情，以便对方了解实际情况。";
    self.reason = @"FINISH_REASON_SUCCEED";
    self.desTextView.text = @"";
    if (self.type == 0) {
        [self requestDataAssurePass];
    }else if (self.type == 1) {
        [self requestDecrypt];
    }else if (self.type == 2) {
        
    }
}
-(void)requestDataAssurePass{
    NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          self.relatedId,@"assurePassId",
                                          nil];
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:dicInfoParams url:@"assurePass/getAssurePassOrder" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.model = [BuyInsuredOrderModel mj_objectWithKeyValues:result[@"data"]];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
    
}
-(void)requestDecrypt{
    __weak typeof(self) weakSelf = self;
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"decryptId":self.relatedId} url:@"decrypt/getDecryptOrder" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            
            self.model = [BuyInsuredOrderModel mj_objectWithKeyValues:result[@"data"]];
            
        }else{
            
            
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}

- (IBAction)typeButtonClick:(UIButton *)sender {
    if (sender == self.button1) {
        self.imageView1.hidden = NO;
        self.imageView2.hidden = YES;
        self.imageView3.hidden = YES;
        self.imageView4.hidden = YES;
        self.imageView5.hidden = YES;
        self.reason = @"FINISH_REASON_SUCCEED";
    }else if (sender == self.button2) {
        self.imageView1.hidden = YES;
        self.imageView2.hidden = NO;
        self.imageView3.hidden = YES;
        self.imageView4.hidden = YES;
        self.imageView5.hidden = YES;
        self.reason = @"FINISH_REASON_FAILED";
    }else if (sender == self.button3) {
        self.imageView1.hidden = YES;
        self.imageView2.hidden = YES;
        self.imageView3.hidden = NO;
        self.imageView4.hidden = YES;
        self.imageView5.hidden = YES;
        self.reason = @"FINISH_REASON_OWN_REASON";
    }else if (sender == self.button4) {
        self.imageView1.hidden = YES;
        self.imageView2.hidden = YES;
        self.imageView3.hidden = YES;
        self.imageView4.hidden = NO;
        self.imageView5.hidden = YES;
        self.reason = @"FINISH_REASON_OTHER_SIDE";
    }else if (sender == self.button5) {
        self.imageView1.hidden = YES;
        self.imageView2.hidden = YES;
        self.imageView3.hidden = YES;
        self.imageView4.hidden = YES;
        self.imageView5.hidden = NO;
        self.reason = @"FINISH_REASON_OTHER";
    }
}

- (IBAction)submitClick:(id)sender {
    if([self.reason isEqualToString:@"FINISH_REASON_SUCCEED"]){
        NSString *url = @"";
        NSString *key = @"";
        if (self.type == 0) {
            url = @"assurePass/finish";
            key = @"assurePassId";
        }else if (self.type == 1) {
            
        }else if (self.type == 2) {
            
        }
        
        
        NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                              [NSString stringWithFormat:@"%zd",self.model.assurePassId],key,
                                              self.reason,@"reasonType",
                                              self.desTextView.text,@"remark",
                                              @"0",@"refundAmount",
                                               [NSString stringWithFormat:@"%f",self.model.price],@"rewardAmount",
                                              @"111111",@"payPwd",
                                              nil];
        
        
        [self showProgressWithView:self.view animated:YES];
        [YJMSendRequest postDataWithParameters:dicInfoParams url:url result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                [self.navigationController popToRootViewControllerAnimated:YES];
                ALERT_VIEW(@"订单交易完成") ;
            }else{
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            
        }];
    }else{
        RefundInsureVC *vc = [[RefundInsureVC alloc]init];
        vc.model = self.model;
        vc.payType = self.reason;
        vc.des = self.desTextView.text;
        vc.type = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
